 
#import "BusinessService.h"
#import "HttpClient.h"

@implementation BusinessService

+ (instancetype)shareInstance
{
    static BusinessService *service;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        service = [[BusinessService alloc] init];
    });
    return service;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.http = [HttpClient shareInstance];
    }
    return self;
}
static NSString * const FORM_FLE_INPUT = @"images";
- (NSMutableURLRequest *)postRequestWithURL: (NSString *)url  // IN
                                 postParems: (NSMutableDictionary *)postParems // IN
                                       data:(NSMutableArray *)datas  // IN
{

    NSLog(@"%@", postParems);
    NSString *hyphens = @"--";
    NSString *boundary = @"*****";
    NSString *end = @"\r\n";
    NSMutableData *myRequestData1=[NSMutableData data];
    //遍历数组，添加多张图片
    if (datas.count != 0) {  //xhw2018-2-12加这个判断
        for (int i = 0; i < datas.count; i ++) {
            NSMutableDictionary *dic = [datas objectAtIndex:i];
            NSData* data = [dic objectForKey:@"value"];
            NSString *key = [dic objectForKey:@"key"];
            //所有字段的拼接都不能缺少，要保证格式正确
            [myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
            [myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
            [myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
            
            NSMutableString *fileTitle=[[NSMutableString alloc]init];
            //要上传的文件名和key，服务器端用file接收
            if([key isEqualToString:@"video"]){
                [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@",key],[NSString stringWithFormat:@"%@.mp4",key]];
            }else{
                if(datas.count == 1){
                    [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@",key],[NSString stringWithFormat:@"%@.png",key]];
                }else{
                    if([key isEqualToString:@"images1"]){
                        [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@",key],[NSString stringWithFormat:@"%@.png",key]];
                    }else{
                        [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@%d",key,i+1],[NSString stringWithFormat:@"%@%d.png",key,i+1]];
                    }
                    
                }
            }
            [fileTitle appendString:end];
            
            [fileTitle appendString:[NSString stringWithFormat:@"Content-Type:application/octet-stream%@",end]];
            [fileTitle appendString:end];
            
            [myRequestData1 appendData:[fileTitle dataUsingEncoding:NSUTF8StringEncoding]];
            
            [myRequestData1 appendData:data];
            
            [myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
            
        }
    }
//    for (int i = 0; i < datas.count; i ++) {
//        NSMutableDictionary *dic = [datas objectAtIndex:i];
//        NSData* data = [dic objectForKey:@"value"];
//        NSString *key = [dic objectForKey:@"key"];
//        //所有字段的拼接都不能缺少，要保证格式正确
//        [myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
//        [myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
//        [myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//
//        NSMutableString *fileTitle=[[NSMutableString alloc]init];
//        //要上传的文件名和key，服务器端用file接收
//        if([key isEqualToString:@"video"]){
//            [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@",key],[NSString stringWithFormat:@"%@.mp4",key]];
//        }else{
//            if(datas.count == 1){
//                [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@",key],[NSString stringWithFormat:@"%@.png",key]];
//            }else{
//                if([key isEqualToString:@"images1"]){
//                    [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@",key],[NSString stringWithFormat:@"%@.png",key]];
//                }else{
//                    [fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"%@%d",key,i+1],[NSString stringWithFormat:@"%@%d.png",key,i+1]];
//                }
//
//            }
//        }
//        [fileTitle appendString:end];
//
//        [fileTitle appendString:[NSString stringWithFormat:@"Content-Type:application/octet-stream%@",end]];
//        [fileTitle appendString:end];
//
//        [myRequestData1 appendData:[fileTitle dataUsingEncoding:NSUTF8StringEncoding]];
//
//        [myRequestData1 appendData:data];
//
//        [myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//
//    }
    
    
    [myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
    [myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
    [myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
    [myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    //参数的集合的所有key的集合
    NSArray *keys= [postParems allKeys];
    
    //添加其他参数
    for(int i=0;i<[keys count];i++)
    {
        NSMutableString *body=[[NSMutableString alloc]init];
        [body appendString:hyphens];
        [body appendString:boundary];
        [body appendString:end];
        //得到当前key
        NSString *key=[keys objectAtIndex:i];
        //添加字段名称
        [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"",key];
        
        [body appendString:end];
        
        [body appendString:end];
        //添加字段的值
        [body appendFormat:@"%@",[postParems objectForKey:key]];
        
        [body appendString:end];
        
        [myRequestData1 appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
        NSLog(@"添加字段的值==%@",[postParems objectForKey:key]);
    }
    
    //根据url初始化request
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:20];
    
    
    //设置HTTPHeader中Content-Type的值
    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",boundary];
    //设置HTTPHeader
    [request setValue:content forHTTPHeaderField:@"Content-Type"];
    //设置Content-Length
    [request setValue:[NSString stringWithFormat:@"%d", [myRequestData1 length]] forHTTPHeaderField:@"Content-Length"];
    //设置http body
    [request setHTTPBody:myRequestData1];
    //http method
    [request setHTTPMethod:@"POST"];
    
//    NSHTTPURLResponse *urlResponese = nil;
//    NSError *error = [[NSError alloc]init];
    
    //        NSData* resultData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponese error:&error];
    
    //    NSString* result= [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
    //    if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){
    //        NSLog(@"返回结果=====%@",result);
    //        [SVProgressHUD dismiss];
    //        return result;
    //    }
    
    return request;
}


@end
